package com.evildog.workspace.system.application.controller;

import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.common.result.Result;
import com.evildog.workspace.common.result.ResultGenerator;
import com.evildog.workspace.dependencies.mybatis.object.BaseConvert;
import com.evildog.workspace.dependencies.mybatis.object.BaseStatusParam;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.system.application.bo.ApiPermissionAllocBO;
import com.evildog.workspace.system.application.bo.ApiPermissionBO;
import com.evildog.workspace.system.application.bo.ApiPermissionListBO;
import com.evildog.workspace.system.application.bo.ApiPermissionPageBO;
import com.evildog.workspace.system.application.convert.ApiPermissionConvert;
import com.evildog.workspace.system.application.model.ApiPermissionPageParam;
import com.evildog.workspace.system.application.model.ApiPermissionParam;
import com.evildog.workspace.system.application.service.ApiPermissionService;
import com.evildog.workspace.system.application.vo.ApiPermissionAllocVO;
import com.evildog.workspace.system.application.vo.ApiPermissionListVO;
import com.evildog.workspace.system.application.vo.ApiPermissionPageVO;
import com.evildog.workspace.system.application.vo.ApiPermissionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * apiPermission Controller
    接口/url权限表,即后端权限（全局权限，极端情况下，包含所有url）
 * </p>
 *
 * @author evildog
 * @since 2020-07-25
 */
@RestController
@RequestMapping("/api-permissions")
@AllArgsConstructor
@Api(tags = "apiPermission相关接口")
public class ApiPermissionController {

    private ApiPermissionService apiPermissionService;

    @PostMapping
    @ApiOperation(value = "添加接口")
    public Result<Boolean> save(@Validated @RequestBody ApiPermissionParam apiPermissionParam) {
        return ResultGenerator.status(apiPermissionService.save(ApiPermissionConvert.INSTANCE.convert(apiPermissionParam)));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public Result<Boolean> removeById(@PathVariable("id") Long id) {
        return ResultGenerator.status(apiPermissionService.remove(id));
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新接口")
    public Result<Boolean> updateById(@PathVariable("id") Long id,@Validated @RequestBody ApiPermissionParam apiPermissionParam) {
        return ResultGenerator.status(apiPermissionService.updateById(id, ApiPermissionConvert.INSTANCE.convert(apiPermissionParam)));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询详情接口")
    public Result<ApiPermissionVO> getById(@PathVariable("id") Long id) {
        ApiPermissionBO apiPermissionBO = apiPermissionService.getDetailById(id);
        return ResultGenerator.success(ApiPermissionConvert.INSTANCE.convert(apiPermissionBO));
    }

    @PatchMapping("/{id}/status")
    @ApiOperation(value = "更新status接口")
    public Result<Boolean> updateStatus(@PathVariable("id") Long id,@Validated @RequestBody BaseStatusParam roleParam) {
        return ResultGenerator.status(apiPermissionService.updateStatus(id, BaseConvert.INSTANCE.convert(roleParam)));
    }


    @GetMapping("/alloc")
    @ApiOperation(value = "获取所有的api list")
    public Result<List<ApiPermissionAllocVO>> getAllocList(){
        List<ApiPermissionAllocBO> apiPermissionListBO = apiPermissionService.getAllocList();
        return ResultGenerator.success(ApiPermissionConvert.INSTANCE.convertAllocVoList(apiPermissionListBO));
    }

    @GetMapping("/page")
    @ApiOperation(value = "表分页查询接口")
    public Result<PagedResult<ApiPermissionPageVO>> getPageList(ApiPermissionPageParam apiPermissionPageParam, PageQuery query){
        PagedResult<ApiPermissionPageBO> apiPermissionPageBO = apiPermissionService.getPageList(
                query,
                QueryConditionUtils.getQueryCondition(apiPermissionPageParam),
                ApiPermissionConvert.INSTANCE.convert(apiPermissionPageParam));
        return ResultGenerator.success(ApiPermissionConvert.INSTANCE.convert(apiPermissionPageBO));
    }

    @GetMapping("/by-menu")
    @ApiOperation(value = "获取所有的api list")
    public Result<List<ApiPermissionListVO>> getListByMenu(@RequestParam Long menuPermissionId){
        List<ApiPermissionListBO> apiPermissionListBO = apiPermissionService.getListByMenu(menuPermissionId);
        return ResultGenerator.success(ApiPermissionConvert.INSTANCE.convertVoList(apiPermissionListBO));
    }

    @GetMapping("/by-component")
    @ApiOperation(value = "获取所有的api list")
    public Result<List<ApiPermissionListVO>> getListByComponentId(@RequestParam Long componentPermissionId){
        List<ApiPermissionListBO> apiPermissionListBO = apiPermissionService.getListByComponentId(componentPermissionId);
        return ResultGenerator.success(ApiPermissionConvert.INSTANCE.convertVoList(apiPermissionListBO));
    }
}